<?php

namespace app\util;
class CurlRequest
{
    /**
     * [CurlRequest 发送CURL请求]
     * @param string  $url    [请求地址]
     * @param boolean $https  [是否为https]
     * @param string  $method [请求方式 post|get]
     * @param string  $data   [请求数据json字符串]
     * @param array   $header [头信息中的参数]
     */
    public static function CurlRequest($url='',$https=true,$method='get',$data=null,$header=array())
    {
        //1.初始化url
        $ch = curl_init($url);
        //2.设置相关的参数
        //字符串不直接输出,进行一个变量的存储
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //判断是否为https请求
        if($https === true){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        //判断是否为post请求
        if($method == 'post'){
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //判断是否为delete请求
        if($method == 'delete'){
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //判断是否有http头参数
        if(!empty($header)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        //3.发送请求
        $str = curl_exec($ch);
        //4.关闭连接
        curl_close($ch);
        //返回请求到的结果
        return $str;
    }

    public static function sendCurlPost($url, $data_string)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
        );
        $ret = curl_exec($curl);
        curl_close($curl);
        return $ret;
    }

    /*
     * 发送HTTP GET 请求方法
     * @param  string $url    请求URL
     * */
    public static function sendCurlGet($url, &$httpCode = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //不做证书校验,部署在linux环境下请改为true
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $file_contents = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $file_contents;
    }

    // curl请求
    public static function sendCurlRequest($url, $data = null, $headers = null, $method='post')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        // CURLOPT_HEADER => true,             // 将头文件的信息作为数据流输出
        // CURLOPT_NOBODY => false,            // true 时将不输出 BODY 部分。同时 Mehtod 变成了 HEAD。修改为 false 时不会变成 GET。
        // CURLOPT_CUSTOMREQUEST => $request->method,  // 请求方法

        if(!empty($data)){
            if (is_array($data)) {
                $data = json_encode($data);
            }
        }

        //判断是否为post请求
        if($method == 'post'){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //判断是否为delete请求
        if($method == 'delete'){
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
        }
        //判断是否为put请求
        if($method == 'put'){
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        }

        if(!empty($headers)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, self::buildHeaders($headers));
        }
        $output = curl_exec($ch);
        // $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        // $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        curl_close($ch);
        return json_decode($output, true);
    }

    // 处理header数据
    public static function buildHeaders($headers)
    {
        $headersArr = array();
        foreach ($headers as $key => $value) {
            array_push($headersArr, "{$key}: {$value}");
        }
        return $headersArr;
    }
}

